package com.neusoft.elmboot.mapper;

import com.neusoft.elmboot.po.PointsRedemption;
import com.neusoft.elmboot.po.PointsStoreItem;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface PointsStoreMapper {
	
	@Select("select * from points_store_item where status = 1 order by pointsRequired asc")
	List<PointsStoreItem> getAllAvailableItems();
	
	// 管理员：获取所有积分商品（不按状态过滤）
	@Select("select * from points_store_item order by itemId asc")
	List<PointsStoreItem> getAllItems();
	
	@Select("select * from points_store_item where itemId = #{itemId}")
	PointsStoreItem getItemById(Integer itemId);
	
	@Update("update points_store_item set stock = stock - 1 where itemId = #{itemId} and stock > 0")
	int decreaseStock(Integer itemId);
	
	// 管理员：新增积分商品
	@Insert("insert into points_store_item (itemName, itemDescription, itemImage, pointsRequired, stock, status) " +
		"values (#{itemName}, #{itemDescription}, #{itemImage}, #{pointsRequired}, #{stock}, #{status})")
	int addItem(PointsStoreItem item);
	
	// 管理员：更新积分商品的基本信息
	@Update("update points_store_item set itemName = #{itemName}, itemDescription = #{itemDescription}, " +
		"pointsRequired = #{pointsRequired}, status = #{status} where itemId = #{itemId}")
	int updateItem(PointsStoreItem item);
	
	// 管理员：仅更新商品状态
	@Update("update points_store_item set status = #{status} where itemId = #{itemId}")
	int updateItemStatus(PointsStoreItem item);
	
	@Insert("insert into points_redemption (userId, itemId, pointsUsed) values (#{userId}, #{itemId}, #{pointsUsed})")
	int insertRedemption(PointsRedemption redemption);
	
	@Select("select * from points_redemption where userId = #{userId} order by redemptionTime desc")
	List<PointsRedemption> getRedemptionsByUserId(String userId);
}

